﻿using J.CustomerRepository.Interface;
using J.StatusModel;
using J.StatusModel.Dto;
using J.StatusRepository;
using J.StatusRepository.Interface;
using StatusDto;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace StatusService
{
    public class LodgingDtoService : ILodgingDtoService
    {
        /// <summary>
        /// 预定
        /// </summary>
        private readonly IBookingRepository bookingRepository;
        /// <summary>
        /// 订单
        /// </summary>
        private readonly IOrdersRepository ordersRepository;
        /// <summary>
        /// 入住
        /// </summary>
        private readonly IStayRepository stayRepository;
        /// <summary>
        /// 关系表
        /// </summary>
        private readonly IBookingRoomRepository bookingRoomRepository;
        public LodgingDtoService(IBookingRepository bookingRepository, IOrdersRepository ordersRepository, IStayRepository stayRepository, IBookingRoomRepository bookingRoomRepository)
        {
            this.stayRepository = stayRepository;
            this.ordersRepository = ordersRepository;
            this.bookingRepository = bookingRepository;
            this.bookingRoomRepository = bookingRoomRepository;
        }
        /// <summary>
        /// 分页显示
        /// </summary>
        /// <param name="lodgingFindDto"></param>
        /// <returns></returns>
        public async Task<PageDto<LodgingDto>> LodgingList(LodgingFindDto lodgingFindDto)
        {
            var BookingList = await bookingRepository.GetAllAsync();
            var OrderList = await ordersRepository.GetAllAsync();
            var StayList= await stayRepository.GetAllAsync();
            var bookRoom = await bookingRoomRepository.GetAllAsync();
            var list = from a in BookingList
                       join b in OrderList on a.ID equals b.BookingId
                       join c in bookRoom on a.ID equals c.BookingId
                       select new LodgingDto
                       {
                           ID = a.ID,
                           RoomId = b.RoomNum,
                           OrderState = b.OrderState,
                           RoomTypeName = b.RoomType,
                           BookingName = a.BookingName,
                           BookingBeginTime = a.BookingBeginTime,
                           BookingEndTime = a.BookingEndTime,
                           BookingTourists = a.BookingTourists,
                           Phone = a.BookingPhone,
                           OrderId = b.ID,
                           RoomTypeId = c.RoomTypeId
                       };

            if (lodgingFindDto.FindNum == 1)
            {
                list = list.Where(x => x.OrderState == OrderState.待入住 && x.BookingBeginTime.Date == DateTime.Now.Date);
            }
            if (lodgingFindDto.FindNum == 2)
            {
                list = list.Where(x => x.OrderState == OrderState.入住中 && x.BookingEndTime.Date == DateTime.Now.Date);
            }
            if (lodgingFindDto.FindNum == 3)
            {
                list = list.Where(x => x.OrderState == OrderState.入住中 && x.BookingBeginTime.Date == DateTime.Now.Date);
            }
            if (lodgingFindDto.FindNum == 4)
            {
                list = list.Where(x => x.OrderState == OrderState.已结算 && x.BookingEndTime.Date == DateTime.Now.Date);
            }
            if (lodgingFindDto.FindNum == 5)
            {
                list = list.Where(x => x.OrderState == OrderState.待入住);
            }
            if (lodgingFindDto.FindNum == 6)
            {
                list = list.Where(x => x.OrderState == OrderState.入住中);
            }
            if (lodgingFindDto.FindNum == 7)
            {
                list = list.Where(x => x.OrderState == OrderState.已退房);
            }
            if (lodgingFindDto.FindNum == 8)
            {
                list = list.Where(x => x.OrderState == OrderState.已结算);
            }
            if (lodgingFindDto.FindNum == 9)
            {
                list = list.Where(x => x.OrderState == OrderState.超时未入住);
            }
            if (lodgingFindDto.FindNum == 10)
            {
                list = list.Where(x => x.OrderState == OrderState.已取消);
            }

            //下单时间
            if (lodgingFindDto.FindNum == 11)
            {
                if (lodgingFindDto.BeginTime != null)
                {
                    list = list.Where(x => x.BookingBeginTime.Date >= lodgingFindDto.BeginTime.Value.AddDays(1));
                }
                if (lodgingFindDto.EndTime != null)
                {
                    list = list.Where(x => x.BookingBeginTime.Date <= lodgingFindDto.EndTime.Value.AddDays(1));
                }
            }
            //入住时间
            if (lodgingFindDto.FindNum == 12)
            {
                if (lodgingFindDto.BeginTime != null)
                {
                    list = list.Where(x => x.BookingBeginTime.Date > lodgingFindDto.BeginTime.Value.Date);
                }
                if (lodgingFindDto.EndTime != null)
                {
                    list = list.Where(x => x.BookingEndTime.Date < lodgingFindDto.EndTime.Value.Date);
                }
            }
            //离店时间
            if (lodgingFindDto.FindNum == 13)
            {
                if (lodgingFindDto.BeginTime != null)
                {
                    list = list.Where(x => x.BookingEndTime.Date >= lodgingFindDto.BeginTime.Value.Date);
                }
                if (lodgingFindDto.EndTime != null)
                {
                    list = list.Where(x => x.BookingEndTime.Date < lodgingFindDto.EndTime.Value.Date);
                }
            }
            //姓名
            if (lodgingFindDto.FindNum == 14)
            {
                if (!string.IsNullOrEmpty(lodgingFindDto.Name))
                {
                    list = list.Where(x => x.BookingName.Contains(lodgingFindDto.Name));
                }
                
            }
            //证件号
            if (lodgingFindDto.FindNum == 15)
            {
                if (!string.IsNullOrEmpty(lodgingFindDto.CardId))
                {
                    list = list.Where(x => x.CardId.Contains(lodgingFindDto.CardId));
                }
            }
            //手机号
            if (lodgingFindDto.FindNum == 16)
            {
                if (!string.IsNullOrEmpty(lodgingFindDto.PhoneNum))
                {
                    list = list.Where(x => x.Phone.Contains(lodgingFindDto.PhoneNum));
                }
            }
            //房号
            if (lodgingFindDto.FindNum == 17)
            {
                if (lodgingFindDto.RoomNum!=0)
                {
                    list = list.Where(x => x.RoomId == lodgingFindDto.RoomNum);
                }            
            }

            int PageTotal = list.Count();
            int PageCount = (int)Math.Ceiling(list.Count() * 1.0 / lodgingFindDto.pageSize);
            list = list.OrderByDescending(x=>x.ID).Skip((lodgingFindDto.pageIndex - 1) * lodgingFindDto.pageSize).Take(lodgingFindDto.pageSize).ToList();

            return new PageDto<LodgingDto>
            {
                Data = list.ToList(),
                pagedCount = PageCount,
                pageIndex = lodgingFindDto.pageIndex,
                pageSize = lodgingFindDto.pageSize,
                totalCount = PageTotal
            };
        }
        /// <summary>
        /// 详情回显
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<List<LodgingAllDto>> LodgingOneData(int Id, OrderState? orderState)
        {
            var BookingList = await bookingRepository.GetAllAsync();
            var OrderList = await ordersRepository.GetAllAsync();
            var StayList = await stayRepository.GetAllAsync();
            var BookRoom = await bookingRoomRepository.GetAllAsync();
            var list = from a in BookingList
                       join b in BookRoom on a.ID equals b.BookingId
                       join c in OrderList on a.ID equals c.BookingId
                       where a.ID==Id
                       select new LodgingAllDto
                       {
                           ID = a.ID,
                           BookingName = a.BookingName,
                           BookingNumber = a.BookingNumber,
                           BookingPhone = a.BookingPhone,
                           BookingOrders = a.BookingOrders,
                           BookingTourists = a.BookingTourists,
                           RoomTypeName = c.RoomType,
                           RoomId = c.RoomNum,
                           BookingBeginTime = a.BookingBeginTime,
                           BookingEndTime = a.BookingEndTime,
                           TimeNumber = a.TimeNumber,
                           OrderState = c.OrderState,
                           CustomizePrice = b.CustomizePrice,
                           BreakfastNum = b.BreakfastNum,
                           BookingText = a.BookingText
                       };
            return list.ToList();
        }
        /// <summary>
        /// 详情回显
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<List<LodgingAllDto>> LodgingOneData2(int Id,OrderState? orderState)
        {
            var BookingList = await bookingRepository.GetAllAsync();
            var OrderList = await ordersRepository.GetAllAsync();
            var StayList = await stayRepository.GetAllAsync();
            var BookRoom = await bookingRoomRepository.GetAllAsync();
            var list = from a in BookingList
                       join b in BookRoom on a.ID equals b.BookingId
                       join c in OrderList on a.ID equals c.BookingId
                       join d in StayList on c.ID equals d.OrdersId
                       where a.ID == Id
                       select new LodgingAllDto
                       {
                           ID = a.ID,
                           BookingName = a.BookingName,
                           BookingNumber = a.BookingNumber,
                           BookingPhone = a.BookingPhone,
                           BookingOrders = a.BookingOrders,
                           BookingTourists = a.BookingTourists,
                           RoomTypeName = c.RoomType,
                           RoomId = c.RoomNum,
                           BookingBeginTime = a.BookingBeginTime,
                           BookingEndTime = a.BookingEndTime,
                           TimeNumber = a.TimeNumber,
                           OrderState = c.OrderState,
                           CustomizePrice = b.CustomizePrice,
                           BreakfastNum = b.BreakfastNum,
                           BookingText = a.BookingText,
                           DocumentsType = d.DocumentsType,
                           DocumentsNumber = d.DocumentsNumber,
                           RoomName = d.RoomName,
                           Phone=d.Phone
                       };
            return list.ToList();
        }
    }
}
